package com.weme.common.manager

import com.google.protobuf.ByteString
import com.google.protobuf.MessageLite
import com.osom.proto.message.NetworkOuterClass
import com.weme.common.ext.create
import com.weme.common.ext.toLittleEndianSizeBuff
import com.weme.common.socket.base.EncoderAdapter
import com.weme.common.socket.base.EncoderCoderOutput
import org.java_websocket.client.WebSocketClient
import java.security.InvalidParameterException


/**
 * Socket 消息编码器
 *
 * @author 
 */
class Encoder : EncoderAdapter {
    var subversion =0L
    override fun encode(client: WebSocketClient?, cmdId:Int,seq :Long,msg: Any?, output: EncoderCoderOutput) {
        subversion += 1;
        if (msg is MessageLite) {
            val netWork = NetworkOuterClass.Network.newBuilder().create(msg, cmdId = cmdId,seq = seq, subversion = subversion)
            output.write(netWork.toLittleEndianSizeBuff())
        }else if(msg is ByteString){
            val netWork = NetworkOuterClass.Network.newBuilder().create(msg.asReadOnlyByteBuffer(), cmdId = cmdId,seq = seq, subversion = subversion)
            output.write(netWork.toLittleEndianSizeBuff())
        } else {
            throw InvalidParameterException("非MessageLite类型的数据类型，需要自己扩展")
        }
    }
}